package SwingComponents;

import Connection.FileWorldCreator;
import Connection.IPServer;
import Controllers.PrincipalController;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

public class PrincipalPanel extends JPanel {

    private PrincipalController principalController;
    private PersonalizedButton playButton;
    private PersonalizedButton selectWorldButton;
    private PersonalizedButton createWorldButton;
    private PersonalizedButton optionsButton;

    public PrincipalPanel(PrincipalController principalController) {
        this.principalController = principalController;
        initComponents();
    }

    private void initComponents() {
        try {
            this.setLayout(new BorderLayout());
            this.setBackground(Color.BLACK);
            TransparentPanel mainPanel = new TransparentPanel();

            IPServer iPServer = new IPServer(principalController.getApplication());
            mainPanel.setBackgroundImage(mainPanel.createImage(new URL(iPServer.getAdress() + "/Images/mainBackground.png")));
            Dimension dimension = new Dimension(501, 701);
            setSize(dimension);
            setPreferredSize(dimension);
            setMinimumSize(dimension);
            setMaximumSize(dimension);

            mainPanel.setSize(dimension);
            mainPanel.setMaximumSize(dimension);
            mainPanel.setMinimumSize(dimension);
            mainPanel.setPreferredSize(dimension);
            mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

            TransparentPanel transparentPanel = new TransparentPanel();
            transparentPanel.setSize(new Dimension(501, 150));
            transparentPanel.setMinimumSize(new Dimension(501, 150));
            transparentPanel.setMaximumSize(new Dimension(501, 150));
            transparentPanel.setPreferredSize(new Dimension(501, 150));

            TransparentPanel endPanel = new TransparentPanel();
            endPanel.setSize(new Dimension(501, 50));
            endPanel.setMaximumSize(new Dimension(501, 50));
            endPanel.setMinimumSize(new Dimension(501, 50));
            endPanel.setPreferredSize(new Dimension(501, 50));           

            JButton loginButton = new JButton("Log in");
            loginButton.setBackground(Color.GRAY);
            loginButton.setBorderPainted(false);
            loginButton.setFocusPainted(false);
            loginButton.addActionListener(principalController);

            JButton registerButton = new JButton("Register");
            registerButton.setBackground(Color.GRAY);
            registerButton.setBorderPainted(false);
            registerButton.setFocusPainted(false);
            registerButton.addActionListener(principalController);
            
            endPanel.add(loginButton);
            endPanel.add(registerButton);

            mainPanel.add(transparentPanel);

            playButton = new PersonalizedButton("Play", iPServer.getAdress() + "/Images/playIcon2.png", iPServer.getAdress() + "/Images/playIcon3.png");
            playButton.setAlignmentX(Component.CENTER_ALIGNMENT);
            playButton.addActionListener(principalController);

            selectWorldButton = new PersonalizedButton("Select World", iPServer.getAdress() + "/Images/worldIcon.png", iPServer.getAdress() + "/Images/worldIcon2.png");
            selectWorldButton.setAlignmentX(Component.CENTER_ALIGNMENT);
            selectWorldButton.addActionListener(principalController);

            createWorldButton = new PersonalizedButton("Create World", iPServer.getAdress() + "/Images/createWorldIcon.png", iPServer.getAdress() + "/Images/createWorldIcon2.png");
            createWorldButton.setAlignmentX(Component.CENTER_ALIGNMENT);
            createWorldButton.addActionListener(principalController);

            optionsButton = new PersonalizedButton("Options", iPServer.getAdress() + "/Images/optionsIcon.png", iPServer.getAdress() + "/Images/optionsIcon2.png");
            optionsButton.setAlignmentX(Component.CENTER_ALIGNMENT);
            optionsButton.addActionListener(principalController);

            mainPanel.add(playButton);
            mainPanel.add(selectWorldButton);
            mainPanel.add(createWorldButton);
            mainPanel.add(optionsButton);

            this.add(mainPanel, BorderLayout.CENTER);
            this.add(endPanel, BorderLayout.PAGE_END);
            this.setVisible(true);
        } catch (Exception e) {
            try {
                File file = new File("C:/PrincipalPanel_initComponents.txt");
                file.createNewFile();
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
                objectOutputStream.writeObject(e.getMessage());
                objectOutputStream.flush();
                objectOutputStream.close();
            } catch (IOException ex1) {
            }
        }
    }
}
